package com.hs.zookeeper.service.impl;

import com.hs.zookeeper.conf.CoreConfig;
import com.hs.zookeeper.service.ZnodeService;
import com.hs.zookeeper.util.ZkUtil;
import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @Author: songhao
 * @Date: 2020/12/29
 * @Description: TODO
 */
public class ZnodeServiceImpl implements ZnodeService {

    static Logger logger = Logger.getLogger(ZnodeServiceImpl.class);

    @Override
    public void createNode(String path, String data,List<ACL> aclList,CreateMode createMode) {
        logger.info("创建节点:[" + path + "]");
        ZooKeeper zkClient = null;
        try{
            zkClient = ZkUtil.getZkClient(CoreConfig.ZK_IP_ADDRESS);
            //如果没有传递acl权限列表的话，就使用默认的权限列表
            if(aclList == null || aclList.size() == 0){
                //获取Acl权限列表
                aclList = ZooDefs.Ids.OPEN_ACL_UNSAFE;
            }
            //创建节点,默认采用所有权限
            zkClient.create(path, data.getBytes(), aclList, createMode);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }finally {
            ZkUtil.close(zkClient);
        }
    }

    @Override
    public String getNodeData(String path){
        String data = null;
        ZooKeeper zkClient = null;
        try{
            zkClient = ZkUtil.getZkClient(CoreConfig.ZK_IP_ADDRESS);
            Stat stat = null;
            if((stat = zkClient.exists(path, true)) != null){
                data = new String(zkClient.getData(path, true, stat));
            }else{
                logger.error("znode===>" + path + ",不存在");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }finally {
            ZkUtil.close(zkClient);
        }
        return data;
    }

    @Override
    public List<String> getChildList(String path){
        List<String> list = null;
        ZooKeeper zkClient = null;
        try{
            zkClient = ZkUtil.getZkClient(CoreConfig.ZK_IP_ADDRESS);
            //获取节点的子节点
            list = zkClient.getChildren( path, false );
        }catch (Exception e) {
            logger.error(e.getMessage(),e);
        }finally {
            ZkUtil.close(zkClient);
        }
        return list;
    }

    @Override
    public void deleteNode(String path){
        logger.info("删除节点:[" + path + "]");
        ZooKeeper zkClient = null;
        try{
            zkClient = ZkUtil.getZkClient(CoreConfig.ZK_IP_ADDRESS);
            Stat stat = null;
            if((stat = zkClient.exists(path, true)) != null){
                List<String> subPaths = zkClient.getChildren(path, false);
                if(subPaths.isEmpty()){
                    zkClient.delete(path, stat.getVersion());
                }else{
                    for (String subPath : subPaths) {
                        deleteNode(path + "/" + subPath);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        } finally {
            ZkUtil.close(zkClient);
        }
    }

    @Override
    public void updateNode(String path, String data) {
        logger.info("更新节点:[" + path + "]");
        ZooKeeper zkClient = null;
        try{
            zkClient = ZkUtil.getZkClient(CoreConfig.ZK_IP_ADDRESS);
            Stat stat = null;
            if ((stat = zkClient.exists(path, true)) != null) {
                zkClient.setData(path, data.getBytes(), stat.getVersion());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }finally {
            ZkUtil.close(zkClient);
        }
    }
}
